using System;
using System.Collections;
using System.Windows.Forms;

namespace Mistral.ComponentModel
{
	public class DataGridViewEditor : IVariantsEditor
	{
		static DataGridView m_dataGridView;
		string rowFilter = String.Empty;
		BindingSource m_bindingSource = new BindingSource( );
		static GridVariantsEditorControl variantsEditorControl;
		public Control CreateEditorControl( ItemViewDelegate viewDelegate )
		{
			if( variantsEditorControl == null )
			{
				variantsEditorControl = new GridVariantsEditorControl();
				
				m_dataGridView = variantsEditorControl.DataGridView;
				m_dataGridView.BorderStyle = BorderStyle.None;
				m_dataGridView.ReadOnly = true;			
				m_dataGridView.RowHeadersVisible = false;
				m_dataGridView.ColumnHeadersVisible = false;
				m_dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
				m_dataGridView.AllowUserToAddRows = false;
				m_dataGridView.AllowUserToDeleteRows = false;
				m_dataGridView.AllowUserToResizeRows = false;
				m_dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
				m_dataGridView.AutoGenerateColumns = true;
				m_dataGridView.MultiSelect = false;

				m_dataGridView.SelectionChanged += new EventHandler( m_dataGridView_SelectionChanged );
				m_dataGridView.KeyPress += new KeyPressEventHandler( m_dataGridView_KeyPress );
				m_dataGridView.KeyDown += new KeyEventHandler( m_dataGridView_KeyDown );				
			}

			
			return variantsEditorControl;
		}

		public void SetItems( ICollection items )
		{
			m_bindingSource.AllowNew = false;
			m_bindingSource.Clear();
			foreach ( object variant in items )
			{
				m_bindingSource.Add( variant );
			}
			m_dataGridView.DataSource = m_bindingSource;
			//m_dataGridView.ClearSelection();			
			//m_dataGridView.AutoResizeColumns( );
			//m_dataGridView.Width = m_dataGridView.Columns[0].Width;
		}
		
		public object SelectedItem
		{
			get { return GetSelectedValue(); }
			set { SetSelectionByValue( value ); }
		}

		public event EventHandler CloseEditor;

		public void SetFilter( string filter )
		{
			
		}

		#region Event Handlers

		void m_dataGridView_SelectionChanged( object sender, EventArgs e )
		{
			if( CloseEditor != null )
				CloseEditor( this, EventArgs.Empty );
		}
		
		private void m_dataGridView_KeyPress( object sender, KeyPressEventArgs e )
		{
//            DataGridView grid = (DataGridView)sender;
			if (Char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == '\\'
			    || e.KeyChar == '_' || e.KeyChar == '(' || e.KeyChar == ')')
			{
				rowFilter += e.KeyChar;
//                string filter;
//                if (curObj is Variant<T>)
//                {
//                    filter = grid.DataTable.Columns[0].ColumnName + " LIKE \'%" + this.rowFilter + "%\'";
//                }
//                else
//                {
//                    filter = grid.DataTable.Columns[0].ColumnName + " LIKE \'%" + this.rowFilter + "%\'";
//                }
				m_bindingSource.Filter = rowFilter;
            	
//                grid.SetFilter(filter);
//                grid.Caption = this.rowFilter;
			}
		}

		private void m_dataGridView_KeyDown( object sender, KeyEventArgs e )
		{
//            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
//            {
//                BaseGrid grid = (BaseGrid)sender;
//                if (rowFilter.Length > 0)
//                {
//                    rowFilter = rowFilter.Substring(0, rowFilter.Length - 1);
//                    string filter;
//                    if (curObj is Variant<T>)
//                    {
//                        filter = grid.DataTable.Columns[0].ColumnName + " LIKE \'%" + this.rowFilter + "%\'";
//                    }
//                    else
//                    {
//                        filter = grid.DataTable.Columns[0].ColumnName + " LIKE \'%" + this.rowFilter + "%\'";
//                    }
//
//                    grid.SetFilter(filter);
//                    grid.Caption = this.rowFilter;
//                }
//            }
		}

		#endregion

		private void SetSelectionByValue( object value )
		{
			foreach ( DataGridViewRow row in m_dataGridView.Rows )
			{
				if ( row.Cells[0].Value == value )
				{
					row.Selected = true;
					m_dataGridView.CurrentCell = row.Cells[0];
					break;
				}
			}
		}
		private object GetSelectedValue()
		{
			object value = null;
			if ( m_dataGridView.SelectedRows.Count > 0 )
			{
				DataGridViewRow row = m_dataGridView.SelectedRows[0];
				value = row.Cells[0].Value;
			}
			return value;
		}
	}
}